<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>tab栏切换操作</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .tab {
            
            width: 1200px;
            height: 330px;
            background-color: royalblue;
        }
        .tab .tab_list {
            height: 30px;
            background-color: slategrey;
        }
        .tab ul li{
            float: left;
            list-style: none;
            cursor: pointer;
            width: 100px;
            height: 30px;
        }
        .color {
            background-color: red;
        }
        .tab .tab_con {
            height: 300px;
            background-color: silver;
        }
    </style>
</head>
<body>
    <div class = "tab">
        <div class="tab_list">
            <ul>
                <li class = "color">商品介绍</li>
                <li>规格与包装</li>
                <li>售后保障</li>
                <li>商品评价</li>
                <li>手机社区</li>
            </ul>
        </div>
        <div class="tab_con">
            <div class="item" style="display:block;">商品介绍</div>
            <div class="item" style="display: none;">规格与包装</div>
            <div class="item" style="display: none;">售后保障</div>
            <div class="item" style="display: none;">商品评价</div>
            <div class="item" style="display: none;">手机社区</div>

        </div>
    </div>

    <script>
        var tab_list = document.querySelector('.tab_list');
        var lis = tab_list.querySelectorAll('li');
        var items = document.querySelectorAll('.item');
        for(var i = 0;i<lis.length;i++){
            //给所有的li添加自定义属性索引号
            lis[i].setAttribute('index',i)
            //设置鼠标点击事件
            lis[i].onclick = function(){
                for( var i = 0;i<lis.length;i++){
                    //将所有类的类名设置为空
                    lis[i].className='';
                    //将所有的display设置为none
                    items[i].style.display='none';
                }
                //给点击事件的类名设置为color
                this.className='color';
                //获取点击事件的索引号
                var index = this.getAttribute('index');
                //将点击事件的display设置为block
                items[index].style.display = 'block';
          }
        }
    </script>
</body>
</html>
